Values X Country

Column

Final Version

Column

About

This plot visualizes the average scores for individualizing and binding foundations across countries.

The values on the x-axis range from 0 (i.e., Strongly Disagree) to 5 (i.e., Strongly Agree).

The final version was improved by (1) modifying the x-axis and (2) explaining the meaning of the colors (i.e., each color represents a country).

Descriptive Statistics

The table below represents the average scores (i.e., mean) for each country as well as standard deviations, minimum and maximum values.

Version 1

This is the first version of the plot. There is a lot to work on here. Both axes and legend labels seem confusing. X-axis scale is also not complete. Also, it is hard to see any patterns without using facet_wrap and sorting the values.

Version 2

The revised version looks much better. You can see interesting patterns such as Spain’s scores. The colors are terrible, though. Also, it’d be nice to see the grand mean to have a general reference category.

Version 3

Based on the peer reviews I received, I realized that there is nothing to indicate what the values on the x-axis and the colors mean.

Predictors of moral values

Column

Final Version

Column

About

The plot describes the fixed effects for the models described above. The coefficients are unstandardized and represented by the dots. The lines represent the 95% confidence intervals.

Findings

Predicting Individualizing and Binding Moral Values
Across 19 Countries
Binding Individualizing
B SE p B SE p
Level 1
Sex [Female] 0.01 0.03 0.8088 0.23 0.03 0.0000
Level 2
Population Sex Ratio −1.18 2.31 0.6286 −2.61 1.77 0.2562
Individualism −0.00 0.01 0.7373 −0.02 0.01 0.0498
Masculinity 0.00 0.00 0.2604 0.01 0.00 0.2149
Gender Equality 2.76 1.40 0.0982 3.22 1.00 0.0965
Human Development Index −3.30 2.87 0.2957 1.82 2.10 0.4782
Overall Life Satisfaction Index 0.44 0.40 0.3125 0.61 0.28 0.1908

Version 1

The initial dot-whisker plot for the fixed effects. Added a vertical line, which made the plot a bit easier to interpret. However, legend looks awful. Modifying the x axis should also help.

Version 2

Easier to see the points. The legend makes sense now, but still could be better. Colors can be improved. Also, there are too many grid lines.

Version 3

Based on the peer review I received, I realized that the order of the variables in the legend is reversed.

Sex X Moral Values for each country

Column

Final Version

Column

About

The plot describes the fixed effects for the models described above. The coefficients are unstandardized and represented by the dots. The lines represent the 95% confidence intervals.

Findings

Predicting Individualizing and Binding Moral Values
Across 19 Countries
Binding Individualizing
B SE p B SE p
Level 1
Sex [Female] 0.01 0.03 0.8088 0.23 0.03 0.0000
Level 2
Population Sex Ratio −1.18 2.31 0.6286 −2.61 1.77 0.2562
Individualism −0.00 0.01 0.7373 −0.02 0.01 0.0498
Masculinity 0.00 0.00 0.2604 0.01 0.00 0.2149
Gender Equality 2.76 1.40 0.0982 3.22 1.00 0.0965
Human Development Index −3.30 2.87 0.2957 1.82 2.10 0.4782
Overall Life Satisfaction Index 0.44 0.40 0.3125 0.61 0.28 0.1908
x predicted group outcome
0.044 3.859424 Australia Individualizing
0.048 3.861192 Australia Individualizing
0.073 3.872245 Australia Individualizing
0.081 3.875782 Australia Individualizing
0.102 3.885066 Australia Individualizing
0.116 3.891256 Australia Individualizing
0.120 3.893024 Australia Individualizing
0.137 3.900540 Australia Individualizing
0.164 3.912477 Australia Individualizing
0.191 3.924414 Australia Individualizing
0.252 3.951383 Australia Individualizing
0.328 3.984984 Australia Individualizing
0.509 4.065006 Australia Individualizing
0.044 2.539436 Australia Binding
0.048 2.546850 Australia Binding
0.073 2.593191 Australia Binding
0.081 2.608020 Australia Binding
0.102 2.646946 Australia Binding
0.116 2.672896 Australia Binding
0.120 2.680311 Australia Binding
0.137 2.711822 Australia Binding
0.164 2.761870 Australia Binding
0.191 2.811918 Australia Binding
0.252 2.924988 Australia Binding
0.328 3.065863 Australia Binding
0.509 3.401368 Australia Binding
0.044 3.354909 Belgium Individualizing
0.048 3.356677 Belgium Individualizing
0.073 3.367730 Belgium Individualizing
0.081 3.371267 Belgium Individualizing
0.102 3.380551 Belgium Individualizing
0.116 3.386741 Belgium Individualizing
0.120 3.388509 Belgium Individualizing
0.137 3.396025 Belgium Individualizing
0.164 3.407962 Belgium Individualizing
0.191 3.419899 Belgium Individualizing
0.252 3.446868 Belgium Individualizing
0.328 3.480469 Belgium Individualizing
0.509 3.560491 Belgium Individualizing
0.044 2.279366 Belgium Binding
0.048 2.286781 Belgium Binding
0.073 2.333121 Belgium Binding
0.081 2.347950 Belgium Binding
0.102 2.386876 Belgium Binding
0.116 2.412827 Belgium Binding
0.120 2.420241 Belgium Binding
0.137 2.451753 Belgium Binding
0.164 2.501800 Belgium Binding
0.191 2.551848 Belgium Binding
0.252 2.664919 Belgium Binding
0.328 2.805794 Belgium Binding
0.509 3.141299 Belgium Binding
0.044 3.284212 China Individualizing
0.048 3.285981 China Individualizing
0.073 3.297033 China Individualizing
0.081 3.300570 China Individualizing
0.102 3.309855 China Individualizing
0.116 3.316044 China Individualizing
0.120 3.317813 China Individualizing
0.137 3.325329 China Individualizing
0.164 3.337266 China Individualizing
0.191 3.349203 China Individualizing
0.252 3.376172 China Individualizing
0.328 3.409772 China Individualizing
0.509 3.489795 China Individualizing
0.044 2.572941 China Binding
0.048 2.580356 China Binding
0.073 2.626696 China Binding
0.081 2.641525 China Binding
0.102 2.680451 China Binding
0.116 2.706402 China Binding
0.120 2.713816 China Binding
0.137 2.745328 China Binding
0.164 2.795375 China Binding
0.191 2.845423 China Binding
0.252 2.958494 China Binding
0.328 3.099369 China Binding
0.509 3.434874 China Binding
0.044 3.433803 France Individualizing
0.048 3.435571 France Individualizing
0.073 3.446624 France Individualizing
0.081 3.450161 France Individualizing
0.102 3.459446 France Individualizing
0.116 3.465635 France Individualizing
0.120 3.467404 France Individualizing
0.137 3.474920 France Individualizing
0.164 3.486857 France Individualizing
0.191 3.498794 France Individualizing
0.252 3.525763 France Individualizing
0.328 3.559363 France Individualizing
0.509 3.639386 France Individualizing
0.044 2.262722 France Binding
0.048 2.270137 France Binding
0.073 2.316477 France Binding
0.081 2.331306 France Binding
0.102 2.370232 France Binding
0.116 2.396183 France Binding
0.120 2.403597 France Binding
0.137 2.435109 France Binding
0.164 2.485157 France Binding
0.191 2.535204 France Binding
0.252 2.648275 France Binding
0.328 2.789150 France Binding
0.509 3.124655 France Binding
0.044 3.605003 Hungary Individualizing
0.048 3.606771 Hungary Individualizing
0.073 3.617824 Hungary Individualizing
0.081 3.621361 Hungary Individualizing
0.102 3.630645 Hungary Individualizing
0.116 3.636835 Hungary Individualizing
0.120 3.638603 Hungary Individualizing
0.137 3.646119 Hungary Individualizing
0.164 3.658056 Hungary Individualizing
0.191 3.669994 Hungary Individualizing
0.252 3.696962 Hungary Individualizing
0.328 3.730563 Hungary Individualizing
0.509 3.810585 Hungary Individualizing
0.044 2.276286 Hungary Binding
0.048 2.283701 Hungary Binding
0.073 2.330041 Hungary Binding
0.081 2.344870 Hungary Binding
0.102 2.383796 Hungary Binding
0.116 2.409747 Hungary Binding
0.120 2.417161 Hungary Binding
0.137 2.448673 Hungary Binding
0.164 2.498720 Hungary Binding
0.191 2.548768 Hungary Binding
0.252 2.661839 Hungary Binding
0.328 2.802714 Hungary Binding
0.509 3.138219 Hungary Binding
0.044 3.440611 Iran Individualizing
0.048 3.442379 Iran Individualizing
0.073 3.453432 Iran Individualizing
0.081 3.456969 Iran Individualizing
0.102 3.466253 Iran Individualizing
0.116 3.472443 Iran Individualizing
0.120 3.474211 Iran Individualizing
0.137 3.481727 Iran Individualizing
0.164 3.493664 Iran Individualizing
0.191 3.505601 Iran Individualizing
0.252 3.532570 Iran Individualizing
0.328 3.566171 Iran Individualizing
0.509 3.646193 Iran Individualizing
0.044 2.383686 Iran Binding
0.048 2.391101 Iran Binding
0.073 2.437441 Iran Binding
0.081 2.452270 Iran Binding
0.102 2.491196 Iran Binding
0.116 2.517147 Iran Binding
0.120 2.524561 Iran Binding
0.137 2.556073 Iran Binding
0.164 2.606121 Iran Binding
0.191 2.656168 Iran Binding
0.252 2.769239 Iran Binding
0.328 2.910114 Iran Binding
0.509 3.245619 Iran Binding
0.044 3.421268 Japan Individualizing
0.048 3.423037 Japan Individualizing
0.073 3.434090 Japan Individualizing
0.081 3.437627 Japan Individualizing
0.102 3.446911 Japan Individualizing
0.116 3.453101 Japan Individualizing
0.120 3.454869 Japan Individualizing
0.137 3.462385 Japan Individualizing
0.164 3.474322 Japan Individualizing
0.191 3.486259 Japan Individualizing
0.252 3.513228 Japan Individualizing
0.328 3.546829 Japan Individualizing
0.509 3.626851 Japan Individualizing
0.044 2.460186 Japan Binding
0.048 2.467600 Japan Binding
0.073 2.513941 Japan Binding
0.081 2.528770 Japan Binding
0.102 2.567696 Japan Binding
0.116 2.593646 Japan Binding
0.120 2.601061 Japan Binding
0.137 2.632572 Japan Binding
0.164 2.682620 Japan Binding
0.191 2.732668 Japan Binding
0.252 2.845738 Japan Binding
0.328 2.986613 Japan Binding
0.509 3.322118 Japan Binding
0.044 3.577148 Latvia Individualizing
0.048 3.578917 Latvia Individualizing
0.073 3.589970 Latvia Individualizing
0.081 3.593506 Latvia Individualizing
0.102 3.602791 Latvia Individualizing
0.116 3.608980 Latvia Individualizing
0.120 3.610749 Latvia Individualizing
0.137 3.618265 Latvia Individualizing
0.164 3.630202 Latvia Individualizing
0.191 3.642139 Latvia Individualizing
0.252 3.669108 Latvia Individualizing
0.328 3.702708 Latvia Individualizing
0.509 3.782731 Latvia Individualizing
0.044 2.439499 Latvia Binding
0.048 2.446913 Latvia Binding
0.073 2.493254 Latvia Binding
0.081 2.508083 Latvia Binding
0.102 2.547009 Latvia Binding
0.116 2.572959 Latvia Binding
0.120 2.580374 Latvia Binding
0.137 2.611885 Latvia Binding
0.164 2.661933 Latvia Binding
0.191 2.711981 Latvia Binding
0.252 2.825051 Latvia Binding
0.328 2.965926 Latvia Binding
0.509 3.301431 Latvia Binding
0.044 3.245465 Netherlands Individualizing
0.048 3.247233 Netherlands Individualizing
0.073 3.258286 Netherlands Individualizing
0.081 3.261823 Netherlands Individualizing
0.102 3.271107 Netherlands Individualizing
0.116 3.277297 Netherlands Individualizing
0.120 3.279065 Netherlands Individualizing
0.137 3.286581 Netherlands Individualizing
0.164 3.298518 Netherlands Individualizing
0.191 3.310455 Netherlands Individualizing
0.252 3.337424 Netherlands Individualizing
0.328 3.371025 Netherlands Individualizing
0.509 3.451047 Netherlands Individualizing
0.044 2.339587 Netherlands Binding
0.048 2.347002 Netherlands Binding
0.073 2.393342 Netherlands Binding
0.081 2.408171 Netherlands Binding
0.102 2.447097 Netherlands Binding
0.116 2.473048 Netherlands Binding
0.120 2.480462 Netherlands Binding
0.137 2.511974 Netherlands Binding
0.164 2.562022 Netherlands Binding
0.191 2.612069 Netherlands Binding
0.252 2.725140 Netherlands Binding
0.328 2.866015 Netherlands Binding
0.509 3.201520 Netherlands Binding
0.044 3.966969 Poland Individualizing
0.048 3.968738 Poland Individualizing
0.073 3.979791 Poland Individualizing
0.081 3.983328 Poland Individualizing
0.102 3.992612 Poland Individualizing
0.116 3.998802 Poland Individualizing
0.120 4.000570 Poland Individualizing
0.137 4.008086 Poland Individualizing
0.164 4.020023 Poland Individualizing
0.191 4.031960 Poland Individualizing
0.252 4.058929 Poland Individualizing
0.328 4.092530 Poland Individualizing
0.509 4.172552 Poland Individualizing
0.044 2.626122 Poland Binding
0.048 2.633536 Poland Binding
0.073 2.679876 Poland Binding
0.081 2.694705 Poland Binding
0.102 2.733631 Poland Binding
0.116 2.759582 Poland Binding
0.120 2.766997 Poland Binding
0.137 2.798508 Poland Binding
0.164 2.848556 Poland Binding
0.191 2.898603 Poland Binding
0.252 3.011674 Poland Binding
0.328 3.152549 Poland Binding
0.509 3.488054 Poland Binding
0.044 3.893027 Spain Individualizing
0.048 3.894796 Spain Individualizing
0.073 3.905848 Spain Individualizing
0.081 3.909385 Spain Individualizing
0.102 3.918670 Spain Individualizing
0.116 3.924859 Spain Individualizing
0.120 3.926628 Spain Individualizing
0.137 3.934144 Spain Individualizing
0.164 3.946081 Spain Individualizing
0.191 3.958018 Spain Individualizing
0.252 3.984987 Spain Individualizing
0.328 4.018587 Spain Individualizing
0.509 4.098610 Spain Individualizing
0.044 2.016348 Spain Binding
0.048 2.023763 Spain Binding
0.073 2.070103 Spain Binding
0.081 2.084932 Spain Binding
0.102 2.123858 Spain Binding
0.116 2.149809 Spain Binding
0.120 2.157223 Spain Binding
0.137 2.188735 Spain Binding
0.164 2.238783 Spain Binding
0.191 2.288830 Spain Binding
0.252 2.401901 Spain Binding
0.328 2.542776 Spain Binding
0.509 2.878281 Spain Binding
0.044 3.643794 Sweden Individualizing
0.048 3.645563 Sweden Individualizing
0.073 3.656615 Sweden Individualizing
0.081 3.660152 Sweden Individualizing
0.102 3.669437 Sweden Individualizing
0.116 3.675626 Sweden Individualizing
0.120 3.677395 Sweden Individualizing
0.137 3.684911 Sweden Individualizing
0.164 3.696848 Sweden Individualizing
0.191 3.708785 Sweden Individualizing
0.252 3.735754 Sweden Individualizing
0.328 3.769354 Sweden Individualizing
0.509 3.849377 Sweden Individualizing
0.044 2.509814 Sweden Binding
0.048 2.517228 Sweden Binding
0.073 2.563569 Sweden Binding
0.081 2.578398 Sweden Binding
0.102 2.617324 Sweden Binding
0.116 2.643274 Sweden Binding
0.120 2.650689 Sweden Binding
0.137 2.682200 Sweden Binding
0.164 2.732248 Sweden Binding
0.191 2.782296 Sweden Binding
0.252 2.895366 Sweden Binding
0.328 3.036241 Sweden Binding
0.509 3.371746 Sweden Binding
0.044 3.815559 Turkey Individualizing
0.048 3.817327 Turkey Individualizing
0.073 3.828380 Turkey Individualizing
0.081 3.831917 Turkey Individualizing
0.102 3.841201 Turkey Individualizing
0.116 3.847391 Turkey Individualizing
0.120 3.849159 Turkey Individualizing
0.137 3.856675 Turkey Individualizing
0.164 3.868612 Turkey Individualizing
0.191 3.880549 Turkey Individualizing
0.252 3.907518 Turkey Individualizing
0.328 3.941119 Turkey Individualizing
0.509 4.021141 Turkey Individualizing
0.044 2.306332 Turkey Binding
0.048 2.313747 Turkey Binding
0.073 2.360087 Turkey Binding
0.081 2.374916 Turkey Binding
0.102 2.413842 Turkey Binding
0.116 2.439793 Turkey Binding
0.120 2.447207 Turkey Binding
0.137 2.478719 Turkey Binding
0.164 2.528767 Turkey Binding
0.191 2.578814 Turkey Binding
0.252 2.691885 Turkey Binding
0.328 2.832760 Turkey Binding
0.509 3.168265 Turkey Binding

Version 1

The initial attempt to visualize the predicted values of moral foundations where the predictor is gender equality. I chose this predictor because it was the only that was significant for both outcomes. I am glad that it worked, but it needs improvement.

Version 2

This version is better, but it would be nicer to see which countries are at the top, middle, and bottom of the plot.

Version 3

---
title: "Moral Values Across Countries"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
    theme: united
---

```{r setup, include=FALSE}
# global options
knitr::opts_chunk$set(echo = FALSE,
                      tidy = TRUE,
                      cache = FALSE,
                      message = FALSE, 
                      error = FALSE, 
                      warning = FALSE)

# packages
library(flexdashboard)
library(here)
library(rio)
library(tidyverse)
library(magrittr)
library(lme4)
library(lmerTest)
library(dotwhisker)
library(tidytext)
library(ggeffects)
library(htmlwidgets)
library(gt)
library(gghighlight)

theme_set(theme_minimal()) # set theme
options(scipen = 999) # remove scientific notation
```

```{r wrangling, include = FALSE}
# import data
df <- import(here("data", "ALL_MFQ30.csv"),     # moral values, countries, & sex
             setclass = "tbl_df") %>% 
  janitor::clean_names()

df_c <- import(here("data", "Data_S1_sec.csv"), # country-level variables
               setclass = "tbl_df") %>% 
  janitor::clean_names()

# data wrangling
df %<>% 
  drop_na() %>%                             
  mutate(
    across(where(is.double), as.numeric),   
    across(where(is.character), as.factor), 
    
    sex = recode(sex,                       
                 `1` = "Male",
                 `0` = "Female",
                 .default = NA_character_),
    
    indiv = rowMeans(                
      select(., harm_avg, fairness_avg) # individualizing moral foundations
    ),
    
    bind = rowMeans(                 
      select(., ingroup_avg:purity_avg) # binding moral foundations
    )
  )

# check data structure and variables
str(df)

# descriptive statistics by country
c_desc <- 
df %>% 
  pivot_longer(cols = c(indiv, bind),
               names_to = "vars",
               values_to = "val"
               ) %>% 
  select(country, vars, val) %>% 
  group_by(country, vars) %>% 
  summarise(mean = mean(val, na.rm = TRUE),
            sd = sd(val, na.rm = TRUE),
            min = min(val, na.rm = TRUE),
            max = max(val, na.rm = TRUE),
            .groups = "drop"
            ) %>% 
  mutate(vars = fct_recode(vars,
                           Individualizing = "indiv",
                           Binding = "bind"
                           )
         )

# descriptive statistics by country and sex
c_s_desc <- 
df %>% 
  filter(country != "Poland") %>%            # Poland has missing data in sex.
  pivot_longer(cols = c(indiv, bind),
               names_to = "vars",
               values_to = "val"
               ) %>% 
  group_by(country, sex, vars) %>% 
  summarise(mean = mean(val, na.rm = TRUE),
            sd = sd(val, na.rm = TRUE),
            min = min(val, na.rm = TRUE),
            max = max(val, na.rm = TRUE),
            .groups = "drop"
            ) %>% 
  mutate(vars = fct_recode(vars,
                           Individualizing = "indiv",
                           Binding = "bind"
                           )
         )
```


# Values X Country

Background {.sidebar}
-----------------------------------------------------------------------
**Data**  
Data used in this dashboard come from the second study of [Atari et al. (2020)](http://dx.doi.org/10.1098/rspb.2020.1201), which can be downloaded from [Kaggle](https://www.kaggle.com/tunguz/sex-differences-in-moral-judgements-67-countries).  

This study has data on moral values in 19 countries. There is also a second dataset with country-level variables.

**Country-level variables**  
  
Population Sex Ratio  
Individualism  
Masculinity  
Gender Equality  
Human Development Index  
Overall Life Satisfaction Index  

**Definitions**

Moral foundations theory [(Graham et al., 2013)](https://doi.org/10.1016/B978-0-12-407236-7.00002-4) argues that there are five universal moral values. These five values have been grouped into two overarching moral values [(Graham et al., 2009)](http://dx.doi.org/10.1037/a0015141) as individualizing and binding moral values.  

*Individualizing* values encompass moral foundations of care (i.e., cherishing and protecting others) and fairness (i.e., rendering justice according to shared rules).  

*Binding* values encompass moral foundations of loyalty (i.e., standing with your group, family, nation), authority (i.e., submitting to tradition and legitimate authority), and purity (i.e., abhorrence for disgusting things, foods, actions).  

**Example Items**  

Please read the following sentences and indicate your agreement or disagreement:  

Individualizing: Compassion for those who are suffering is the most crucial virtue.  

Binding: People should be loyal to their family members, even when they have done something wrong.  

Column {data-width=600}
-----------------------------------------------------------------------

### **Final Version**

```{r}
gmeans <- 
c_desc %>% 
  group_by(vars) %>% 
  summarise(m = mean(mean))

c_desc %>% 
  ggplot() + 
  geom_vline(
    data = gmeans,
    aes(xintercept = m), 
    linetype = 2,
    alpha = .6
            ) + 
  geom_vline(
    data = data.frame(x = seq(0,5,1)),
    aes(xintercept = x),
        color = "gray80",
        alpha = .6
             ) +
  geom_hline(aes(yintercept = 0.5),
             alpha = .6,
             color = "gray80") +
  geom_col(
    aes(mean, reorder_within(country, mean, vars), 
        fill = country
        ),
    alpha = .7,
    position = position_nudge(y = -0.05)
          ) + 
  scale_y_reordered() + 
  scale_x_continuous(expand = c(0, 0.32),
                     limits = c(0, 5),
                     breaks = c(0, 5),
                     labels = c("Strongly\nDisagree",
                                "Strongly\nAgree")
                     ) +
  facet_wrap(~factor(vars,
                     levels = c("Binding",
                                "Individualizing"
                                )
                     ), 
             scales = "free_y",
             ncol = 2) + 
  theme(
    plot.title.position = "plot",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "none",
    axis.text.y = element_text(color = "black",
                               size = 11),
    axis.text.x = element_text(color = "black",
                               size = 8,
                               hjust = .5),
    axis.title = element_blank(),
    strip.background.x = element_rect(color = "gray80",
                                      size = 1),
    strip.text.x = element_text(face = "bold")
  )  + 
  labs(
    title = "Endorsement of Individualizing and Binding Moral Values Across Countries",
    caption = "\n\nDashed lines represent the average of all countries.\nColors represent the countries."
    ) 
```

Column {.tabset .tabset-fade data-width=400}
-----------------------------------------------------------------------
### About
This plot visualizes the average scores for individualizing and binding foundations across countries.  

The values on the x-axis range from 0 (i.e., Strongly Disagree) to 5 (i.e., Strongly Agree).  

The final version was improved by (1) modifying the x-axis and (2) explaining the meaning of the colors (i.e., each color represents a country).  

**Descriptive Statistics**  
  
The table below represents the average scores (i.e., mean) for each country as well as standard deviations, minimum and maximum values.  


```{r}
DT::datatable(c_desc,
              colnames = c("Country", "Moral Value",
                           "Mean", "SD", "Min", "Max"),
              rownames = FALSE,
              filter = "none",
              options = list(scrollX = TRUE,
                             pageLength = 6,
                             searching = FALSE,
                             columnDefs = list(list(className = "dt-center",
                                                    targets = "_all")
                                               ),
                             lengthChange = FALSE,
                               initComplete = JS(
                               "function(settings, json) {",
                               "$(this.api().table().header()).css({'background-color': '#9f939c', 'color': '#fff'});",
                               "}")
                             )
              ) %>% 
  DT::formatRound(columns = c("mean", "sd", "min", "max"),
                  digits = 2)
```


### Version 1

```{r}
c_desc %>% 
  ggplot() + 
  geom_col(
    aes(mean, country, fill = vars),
    position = "dodge"
  ) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank()
  ) + 
  scale_x_continuous(expand = c(0, 0))
```

> This is the first version of the plot. There is a lot to work on here. Both axes and legend labels seem confusing. X-axis scale is also not complete. Also, it is hard to see any patterns without using facet_wrap and sorting the values.

### Version 2

```{r}
c_desc %>% 
  ggplot() + 
  geom_col(
    aes(mean, reorder_within(country, mean, vars)
        )
          ) + 
  scale_y_reordered() + 
  scale_x_continuous(expand = c(0, 0)
                     ) +
  facet_wrap(~vars, 
             scales = "free_y",
             ncol = 2) +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    axis.text.y = element_text(color = "black",
                               size = 11),
    axis.text.x = element_text(color = "black",
                               size = 9),
    axis.title = element_blank()
  )  + 
  labs(
    title = "Endorsement of Individualizing and Binding Moral Values Across Countries"
    )
```

> The revised version looks much better. You can see interesting patterns such as Spain's scores. The colors are terrible, though. Also, it'd be nice to see the grand mean to have a general reference category.

### Version 3

```{r}
c_desc %>% 
  ggplot() + 
  geom_vline(data = gmeans,
             aes(xintercept = m), 
             linetype = 2,
             alpha = .6) +   
  geom_col(
    aes(mean, reorder_within(country, mean, vars), 
        fill = country,
        alpha = .9
        )
          ) + 
  scale_y_reordered() + 
  scale_x_continuous(expand = c(0, 0)
                     ) +
  facet_wrap(~vars, 
             scales = "free_y",
             ncol = 2) + 
  theme(
    plot.title.position = "plot",
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    legend.position = "none",
    axis.text.y = element_text(color = "black",
                               size = 11),
    axis.text.x = element_text(color = "black",
                               size = 9),
    axis.title = element_blank()
  )  + 
  labs(
    title = "Endorsement of Individualizing and Binding Moral Values Across Countries",
    caption = "Vertical lines represent the average of all countries."
    ) 
```

> Based on the peer reviews I received, I realized that there is nothing to indicate what the values on the x-axis and the colors mean.


# Predictors of moral values  

Background {.sidebar}
-----------------------------------------------------------------------
**Data**  
Data used in this dashboard come from the second study of [Atari et al. (2020)](http://dx.doi.org/10.1098/rspb.2020.1201), which can be downloaded from [Kaggle](https://www.kaggle.com/tunguz/sex-differences-in-moral-judgements-67-countries).  

This study has data on moral values in 19 countries. There is also a second dataset with country-level variables.

**Country-level variables**  
  
Population Sex Ratio  
Individualism  
Masculinity  
Gender Equality  
Human Development Index  
Overall Life Satisfaction Index  

**Definitions**

Moral foundations theory [(Graham et al., 2013)](https://doi.org/10.1016/B978-0-12-407236-7.00002-4) argues that there are five universal moral values. These five values have been grouped into two overarching moral values [(Graham et al., 2009)](http://dx.doi.org/10.1037/a0015141) as individualizing and binding moral values.  

*Individualizing* values encompass moral foundations of care (i.e., cherishing and protecting others) and fairness (i.e., rendering justice according to shared rules).  

*Binding* values encompass moral foundations of loyalty (i.e., standing with your group, family, nation), authority (i.e., submitting to tradition and legitimate authority), and purity (i.e., abhorrence for disgusting things, foods, actions).  

**Example Items**  

Please read the following sentences and indicate your agreement or disagreement:  

Individualizing: Compassion for those who are suffering is the most crucial virtue.  

Binding: People should be loyal to their family members, even when they have done something wrong.  

Column {data-width=600}
-----------------------------------------------------------------------

```{r MLM, include=FALSE}
# merge country-level data with the main dataset
df <- left_join(df, 
                select(df_c, country:overall_life_satisfaction_index), 
                by = "country")

# MLM
model_i <- lmer(indiv ~ relevel(sex, ref = 2) + # level 1 predictor
                  
                  # level 2 predictors:
                  pop_sex_ratio + individualism + masculinity + 
                  gender_eqality + human_development_index + 
                  overall_life_satisfaction_index + 
                  
                  (sex | country), # random slope for sex
                                   # random intercept for country
                
                data = df
                )

model_b <- lmer(bind ~ relevel(sex, ref = 2) + # level 1 predictor
                  
                  # level 2 predictors:
                  pop_sex_ratio + individualism + masculinity + 
                  gender_eqality + human_development_index + 
                  overall_life_satisfaction_index + 
                  
                  (sex | country), # random slope for sex
                                   # random intercept for country
                
                data = df
                )

# extract coefficients
m_i_fixed <-
broom.mixed::tidy(model_i) %>% 
  filter(effect == "fixed",
         term != "(Intercept)") %>% 
  select(-c(effect, group)) %>% 
  mutate(
         term = recode(term,
                       `relevel(sex, ref = 2)Female` = "Sex [Female]",
                       `pop_sex_ratio` = "Population Sex Ratio",
                       `individualism` = "Individualism",
                       `masculinity` = "Masculinity",
                       `gender_eqality` = "Gender Equality",
                       `human_development_index` = "Human Development Index",
                       `overall_life_satisfaction_index` = "Overall Life Satisfaction Index"),
         model = "Individualizing",
  ) %>% 
  relocate(model, term)

m_b_fixed <-
broom.mixed::tidy(model_b) %>% 
  filter(effect == "fixed",
         term != "(Intercept)") %>% 
  select(-c(effect, group)) %>% 
  mutate(
         term = recode(term,
                       `relevel(sex, ref = 2)Female` = "Sex [Female]",
                       `pop_sex_ratio` = "Population Sex Ratio",
                       `individualism` = "Individualism",
                       `masculinity` = "Masculinity",
                       `gender_eqality` = "Gender Equality",
                       `human_development_index` = "Human Development Index",
                       `overall_life_satisfaction_index` = "Overall Life Satisfaction Index"),
         model = "Binding",
  ) %>% 
  relocate(model, term)

both_ms <- bind_rows(m_i_fixed, m_b_fixed)
```


### **Final Version**

```{r}
# Tried to reorder the y axis, but this didn't work for some reason.
# both_ms %<>% 
#   mutate(
#     term = factor(term, 
#                   levels = c("Sex [Female]",
#                              "Population Sex Ratio",
#                              "Individualism",
#                              "Masculinity",
#                              "Gender Equality",
#                              "Human Development Index",
#                              "Overall Life Satisfaction Index"
#                             )
#                   )
#         )


ggplot() + 
  
  geom_vline(xintercept = 0, 
             colour = "grey60", 
             linetype = 2) +  
  
  geom_errorbarh(data = filter(both_ms, model == "Individualizing"),
                 aes(xmin = estimate + qnorm(0.025)*std.error,
                     xmax = estimate + qnorm(0.975)*std.error,
                     y = term,
                     color = "Individualizing"),
                 position = position_nudge(y = -.1),
                 height = 0
                 ) +
  
  geom_point(data = filter(both_ms, model == "Individualizing"),
             aes(estimate, term, color = "Individualizing"),
             position = position_nudge(y = -.1),
             size = 1.75
             ) +
  
  geom_errorbarh(data = filter(both_ms, model == "Binding"),
                 aes(xmin = estimate - 1.96*std.error,
                     xmax = estimate + 1.96*std.error,
                     y = term,
                     color = "Binding"),
                 position = position_nudge(y = .1),
                 height = 0
                 ) +
  
  geom_point(data = filter(both_ms, model == "Binding"),
             aes(estimate, term, color = "Binding"),
             position = position_nudge(y = .1),
             size = 1.75
             ) + 
  
  scale_color_manual(name = "",
                     values = c("Individualizing" = "cornflowerblue",
                                "Binding" = "#F8766D")
                     ) +
  
  scale_x_continuous(n.breaks = 10) +
  
  theme(plot.title = element_text(vjust = 3),
        plot.title.position = "plot",
        axis.text.y = element_text(color = "black",
                                   size = 11),
        axis.title.y = element_blank(),
        legend.justification = c(0, 0), 
        legend.position = c(.65, .85),
        legend.background = element_rect(colour = "grey80"),
        legend.title = element_blank(),
        panel.grid.major.y = element_blank()
        )  +
  
  labs(title = "Predicting moral values by sex and country-level predictors",
       x = "Unstandardized Coefficient")
  
```

Column {.tabset .tabset-fade data-width=400}
-----------------------------------------------------------------------
### About  

The plot describes the fixed effects for the models described above. The coefficients are unstandardized and represented by the dots. The lines represent the 95% confidence intervals.  

**Findings**  
  
```{r}
table <- 
  bind_cols(
    
    subset(both_ms, model == "Binding",
           select = c(term:std.error, p.value)
           ),
    
    subset(both_ms, model == "Individualizing",
           select = c(term:std.error, p.value)
           )
  ) %>% 
  rename(
    term1 = 1, term2 = 5,
    est1 = 2, est2 = 6,
    se1 = 3, se2 = 7,
    p1 = 4, p2 = 8
  )

table %>% 
  select(-term2) %>% 
  mutate(gr = c("Level 1", rep("Level 2", 6))) %>% 
  group_by(gr) %>% 
  gt() %>% 
  tab_header(
    title = "Predicting Individualizing and Binding Moral Values",
    subtitle = "Across 19 Countries"
  ) %>% 
  cols_label(term1 = "",
             est1 = md("*B*"),
             se1 = "SE",
             p1 = "p",
             est2 = md("*B*"),
             se2 = "SE",
             p2 = "p") %>% 
  tab_spanner(
    label = "Binding",
    columns = vars(est1, se1, p1)
  ) %>% 
  tab_spanner(
    label = "Individualizing",
    columns = vars(est2, se2, p2)
  ) %>% 
  fmt_number(columns = vars(est1, se1,
                            est2, se2),
             decimals = 2) %>% 
  fmt_number(columns = vars(p1, p2),
             decimals = 4
             ) %>% 
  cols_align(
    columns = "term1",
    align = "right"
  ) %>% 
  opt_row_striping() %>% 
  tab_options(
    heading.background.color = "#9f939c",
    table.font.size = "100%"
    ) %>% 
  tab_style(
    style = list(
      cell_text(weight = "bold")
    ),
    locations = cells_body(
      columns = vars(p1),
      rows = p1 < .05
    )
  )
```



### Version 1

```{r}
dwplot(both_ms) + 
  ggtitle("Predicting moral values") +
  xlab("Unstandardized Coefficient") + 
  geom_vline(xintercept = 0, 
             colour = "grey60", 
             linetype = 2)
```

> The initial dot-whisker plot for the fixed effects. Added a vertical line, which made the plot a bit easier to interpret. However, legend looks awful. Modifying the x axis should also help.

### Version 2

```{r}
dwplot(both_ms, 
        dot_args = list(size = 2)
        ) + 
  ggtitle("Predicting moral values by sex and country-level predictors") +
  xlab("Unstandardized Coefficient") + 
  geom_vline(xintercept = 0, 
             colour = "grey60", 
             linetype = 2) + 
  theme(plot.title = element_text(face = "bold"),
        plot.title.position = "plot",
        axis.text.y = element_text(color = "black",
                                   size = 11),
        legend.justification = c(0, 0), 
        legend.position = c(.74, .85),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
        ) +
  scale_x_continuous(n.breaks = 10) +
  scale_color_manual(values = c("blue", "red"))
```

> Easier to see the points. The legend makes sense now, but still could be better. Colors can be improved. Also, there are too many grid lines.

### Version 3

```{r}
dwplot(both_ms, 
        dot_args = list(size = 2)
        ) + 
  ggtitle("Predicting moral values by sex and country-level predictors") +
  xlab("Unstandardized Coefficient") + 
  geom_vline(xintercept = 0, 
             colour = "grey60", 
             linetype = 2) + 
  theme(plot.title = element_text(face = "bold", vjust = 3),
        plot.title.position = "plot",
        axis.text.y = element_text(color = "black",
                                   size = 11),
        legend.justification = c(0, 0), 
        legend.position = c(.65, .85),
        legend.background = element_rect(colour = "grey80"),
        legend.title = element_blank(),
        panel.grid.major.y = element_blank()
        ) +
  scale_x_continuous(n.breaks = 10) +
  scale_color_manual(values = c("cornflowerblue", "#F8766D"))
```

> Based on the peer review I received, I realized that the order of the variables in the legend is reversed.

# Sex X Moral Values for each country

Background {.sidebar}
-----------------------------------------------------------------------
**Data**  
Data used in this dashboard come from the second study of [Atari et al. (2020)](http://dx.doi.org/10.1098/rspb.2020.1201), which can be downloaded from [Kaggle](https://www.kaggle.com/tunguz/sex-differences-in-moral-judgements-67-countries).  

This study has data on moral values in 19 countries. There is also a second dataset with country-level variables.

**Country-level variables**  
  
Population Sex Ratio  
Individualism  
Masculinity  
Gender Equality  
Human Development Index  
Overall Life Satisfaction Index  

**Definitions**

Moral foundations theory [(Graham et al., 2013)](https://doi.org/10.1016/B978-0-12-407236-7.00002-4) argues that there are five universal moral values. These five values have been grouped into two overarching moral values [(Graham et al., 2009)](http://dx.doi.org/10.1037/a0015141) as individualizing and binding moral values.  

*Individualizing* values encompass moral foundations of care (i.e., cherishing and protecting others) and fairness (i.e., rendering justice according to shared rules).  

*Binding* values encompass moral foundations of loyalty (i.e., standing with your group, family, nation), authority (i.e., submitting to tradition and legitimate authority), and purity (i.e., abhorrence for disgusting things, foods, actions).  

**Example Items**  

Please read the following sentences and indicate your agreement or disagreement:  

Individualizing: Compassion for those who are suffering is the most crucial virtue.  

Binding: People should be loyal to their family members, even when they have done something wrong.   


Column {data-width=600}
-----------------------------------------------------------------------

### Final Version

```{r}
ms1 <- lmer(indiv ~ sex + (sex|country), data = df)
ms2 <- lmer(bind ~ sex + (sex|country), data = df)

p1 <- ggpredict(ms1,
                terms = c("sex", "country"),
                type = "re"
                )

p2 <- ggpredict(ms2,
                terms = c("sex", "country"),
                type = "re"
                )

p1 %<>% 
  as.data.frame() %>% 
  mutate(
    outcome = "Individualizing"
  )

p2 %<>% 
  as.data.frame() %>% 
  mutate(
    outcome = "Binding"
  )

p_combined <- 
  bind_rows(p1, p2) %>% 
  rename(Sex = x)

ggplot(p_combined) + 
  geom_line(aes(predicted, group),
            size = 2.5,
            color = "#535353") + 
  geom_point(aes(predicted, group, color = Sex),
             size = 4) + 
  geom_hline(yintercept = 0.5,
             color = "gray90") +
  facet_wrap(~outcome) +
  theme(
    plot.title.position = "plot",
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    legend.position = c(0.1, -.2),
    legend.direction = "horizontal",
    axis.text.y = element_text(color = "black",
                               size = 11),
    axis.text.x = element_text(color = "black",
                               size = 8,
                               hjust = .5),
    axis.title.y = element_blank(),
    axis.title.x = element_text(vjust = -2),
    strip.background.x = element_rect(color = "gray80",
                                      size = 1),
    strip.text.x = element_text(face = "bold")
  )  + 
  labs(
    title = "Predicted values of Individualizing and Binding Moral Values by Sex",
    caption = "\nDashed lines represent the average predicted values of all countries.",
    x = "Predicted Values"
    ) +
  scale_color_manual(values = c("#95c3db", "#1a64a4"))
```


```{r}
# run the models
model1 <- lmer(indiv ~ gender_eqality + (sex|country), data = df)
model2 <- lmer(bind ~ gender_eqality + (sex|country), data = df)

# extract the predicted values
predicted1 <- 
  ggpredict(model1, 
            terms = c("gender_eqality", "country"),
            type = "re")

predicted2 <- 
  ggpredict(model2, 
            terms = c("gender_eqality", "country"),
            type = "re")

predicted1 %<>% 
  as.data.frame() %>% 
  mutate(
    outcome = "Individualizing"
  )

predicted2 %<>% 
  as.data.frame() %>% 
  mutate(
    outcome = "Binding"
  )

pred_combined <- bind_rows(predicted1, predicted2)

pred_combined %>% 
  ggplot(aes(x, predicted, color = group)) +
  geom_line(size = .75) + 
  facet_wrap(~ outcome) +
  labs(title = "Relationship between gender equality and moral values",
       x = "Gender Equality",
       y = "Predicted Values",
       color = "Country") +
  theme(
    plot.title.position = "plot",
    axis.text = element_text(size = 10,
                             colour = "black"),    
    strip.background.x = element_rect(color = "gray80",
                                  size = 1),
    strip.text.x = element_text(face = "bold",
                                size = 11)
        
        ) +
  gghighlight(group %in% c("Japan", "Netherlands", "Spain", "Iran"),
               calculate_per_facet = TRUE)
```

Column {.tabset .tabset-fade data-width=400}
-----------------------------------------------------------------------

### About

The plot describes the fixed effects for the models described above. The coefficients are unstandardized and represented by the dots. The lines represent the 95% confidence intervals.  

**Findings**  
  
```{r}
table <- 
  bind_cols(
    
    subset(both_ms, model == "Binding",
           select = c(term:std.error, p.value)
           ),
    
    subset(both_ms, model == "Individualizing",
           select = c(term:std.error, p.value)
           )
  ) %>% 
  rename(
    term1 = 1, term2 = 5,
    est1 = 2, est2 = 6,
    se1 = 3, se2 = 7,
    p1 = 4, p2 = 8
  )

table %>% 
  select(-term2) %>% 
  mutate(gr = c("Level 1", rep("Level 2", 6))) %>% 
  group_by(gr) %>% 
  gt() %>% 
  tab_header(
    title = "Predicting Individualizing and Binding Moral Values",
    subtitle = "Across 19 Countries"
  ) %>% 
  cols_label(term1 = "",
             est1 = md("*B*"),
             se1 = "SE",
             p1 = "p",
             est2 = md("*B*"),
             se2 = "SE",
             p2 = "p") %>% 
  tab_spanner(
    label = "Binding",
    columns = vars(est1, se1, p1)
  ) %>% 
  tab_spanner(
    label = "Individualizing",
    columns = vars(est2, se2, p2)
  ) %>% 
  fmt_number(columns = vars(est1, se1,
                            est2, se2),
             decimals = 2) %>% 
  fmt_number(columns = vars(p1, p2),
             decimals = 4
             ) %>% 
  cols_align(
    columns = "term1",
    align = "right"
  ) %>% 
  opt_row_striping() %>% 
  tab_options(
    heading.background.color = "#9f939c",
    table.font.size = "100%"
    ) %>% 
  tab_style(
    style = list(
      cell_text(weight = "bold")
    ),
    locations = cells_body(
      columns = vars(p1),
      rows = p1 < .05
    )
  )

pred_combined %>% 
  arrange(group) %>% 
  gt()
```

### Version 1

```{r}
# plot
p1 <- 
predicted1 %>% 
  ggplot(aes(x, predicted, color = group)) +
  geom_line(size = 1)

p2 <- 
predicted2 %>% 
  ggplot(aes(x, predicted, color = group)) +
  geom_line(size = 1)

ggpubr::ggarrange(p1, p2,
                  common.legend = TRUE,
                  legend = "bottom"
                 ) 
```

> The initial attempt to visualize the predicted values of moral foundations where the predictor is gender equality. I chose this predictor because it was the only that was significant for both outcomes. I am glad that it worked, but it needs improvement.

### Version 2

```{r}
p1 <-
p1 + 
  labs(x = "Gender Equality",
       y = "Individualizing",
       color = "Country") +
  theme(axis.text = element_text(size = 10,
                                 colour = "black"))

p2 <-
p2 + 
  labs(x = "Gender Equality",
       y = "Binding",
       color = "Country") +
  theme(axis.text = element_text(size = 10,
                                 colour = "black"))

ggpubr::ggarrange(p1, p2,
                  common.legend = TRUE,
                  legend = "bottom"
                 ) 
```

> This version is better, but it would be nicer to see which countries are at the top, middle, and bottom of the plot.


### Version 3

```{r}
# run the models
model1 <- lmer(indiv ~ gender_eqality + (sex|country), data = df)
model2 <- lmer(bind ~ gender_eqality + (sex|country), data = df)

# extract the predicted values
predicted1 <- 
  ggpredict(model1, 
            terms = c("gender_eqality", "country"),
            type = "re")

predicted2 <- 
  ggpredict(model2, 
            terms = c("gender_eqality", "country"),
            type = "re")

p11 <- 
  predicted1 %>% 
  ggplot(aes(x, predicted, color = group)) +
  geom_line(size = 1) +
  labs(x = "Gender Equality",
       y = "Individualizing",
       color = "Country") +
  theme(axis.text = element_text(size = 10,
                                 colour = "black")
        ) +
  gghighlight::gghighlight(group %in% c("Poland", "Netherlands", "Hungary"))

p21 <- 
  predicted2 %>% 
  ggplot(aes(x, predicted, color = group)) +
  geom_line(size = 1) +
  labs(x = "Gender Equality",
       y = "Binding",
       color = "Country") +
  theme(axis.text = element_text(size = 10,
                                 colour = "black")
        ) +
  gghighlight::gghighlight(group %in% c("Poland", "Netherlands", "Spain"))

ggpubr::ggarrange(p11, p21,
                  common.legend = TRUE,
                  legend = "bottom")  %>%  
  ggpubr::annotate_figure(
    top = ggpubr::text_grob("Countries at the top, middle, and bottom")
    )
```